<template>
  <div class="find">
    <hm-nav-bar title="发现" icon=""></hm-nav-bar>
    <van-pull-refresh v-model="loading" @refresh="refreshEvent">
      <div>
        <div class="content">
          <findCell title="面试技巧"></findCell>
          <technic-item
            v-for="(item, index) in TechnicList"
            :key="index"
            :info="item"
          ></technic-item>
        </div>

        <div class="line"></div>

        <div class="content">
          <findCell title="市场数据" class="shuju"></findCell>
          <van-tag class="tag1" color="#eceaea" text-color="#858498">{{
            chart.city
          }}</van-tag>
          <van-tag class="tag2" color="#eceaea" text-color="#858498">{{
            chart.position
          }}</van-tag>
          <ul class="chart">
            <li
              class="chart-item"
              v-for="(item, index) in chart.yearSalary"
              :key="index"
              v-show="index < showTotal"
            >
              <div class="c1">{{ item.year.substr(0, 5) }}</div>
              <div class="c2">
                <div
                  class="num"
                  :style="{
                    width: (item.salary / chart.topSalary) * 100 + '%'
                  }"
                >
                  ￥{{ item.salary }}
                </div>
              </div>
              <div class="c3">
                <div v-if="item.percent !== undefined">
                  <i class="up iconfont" v-if="item.percent <= 0">&#xe64e;</i>
                  <i class="down iconfont" v-else>&#xe651;</i>
                </div>
              </div>
              <div class="c4">
                <span v-if="item.percent !== undefined"
                  >{{ item.percent }}%</span
                >
              </div>
            </li>
          </ul>
          <div class="look-more">
            <span v-if="showTotal === 3" @click="showTotal = 100" class="m1"
              >展开更多<i class="iconfont">&#xe652;</i></span
            >
            <span v-else @click="showTotal = 3" class="m2"
              >收起<i class="iconfont">&#xe652;</i></span
            >
          </div>
        </div>
        <div class="line"></div>

        <div class="content">
          <findCell
            title="面经分享"
            @click.native="$router.push('/home/shareList')"
          ></findCell>
          <shareltem
            v-for="(item, index) in sharelist"
            :key="index"
            :info="item"
          ></shareltem>
        </div>
      </div>
    </van-pull-refresh>
  </div>
</template>

<script>
import findCell from './findCell.vue'
import TechnicItem from './technicItem'
import shareltem from './shareltem'
import { articlesTechnic, chartDataHot, articlesShare } from '@/api/find.js'
export default {
  components: { findCell, TechnicItem, shareltem },
  name: 'find',
  data () {
    return {
      TechnicList: [],
      chart: '', // 市场数据
      showTotal: 3, // 市场数据显示多条数据
      sharelist: '', // 保存面经分享数据
      loading: false // 是否处于加载中
    }
  },
  // 在勾子中发axios请求
  async created () {
    const res = await articlesTechnic()
    this.TechnicList = res.data.data.list
    console.log('res1', res)

    const res2 = await chartDataHot() // 调用市场数据
    console.log('res2', res2)
    res2.data.data.yearSalary.reverse() // 拿到数据 reverse（反转数据）
    this.chart = res2.data.data // 存储数据

    const res3 = await articlesShare() // 调用面经分享
    console.log('res3', res3)
    this.sharelist = res3.data.data.list
  },

  methods: {
    // 刷新方法
    async refreshEvent () {
      Promise.all([articlesTechnic(), chartDataHot(), articlesShare()]).then(
        Response => {
          const res = Response[0]
          this.TechnicList = res.data.data.list
          console.log('res1', res)

          const res2 = Response[1] // 调用市场数据
          console.log('res2', res2)
          res2.data.data.yearSalary.reverse() // 拿到数据 reverse（反转数据）
          this.chart = res2.data.data // 存储数据

          const res3 = Response[2] // 调用面经分享
          console.log('res3', res3)
          this.sharelist = res3.data.data.list
          // 让是否处于加载中 该成加载完成
          this.loading = false
        }
      )
    }
  }
}
</script>

<style lang="less" scoped>
.find {
  .line {
    width: 100%;
    height: 10px;
    background-color: #f7f4f5;
  }
  .content {
    padding: 0 15px;
    .shuju {
      margin-bottom: 10px;
    }
    .tag1 {
      width: 41px;
      height: 24px;
      background: #eceaea;
      border-radius: 4px;
      font-size: 14px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: center;
      color: #545671;
      line-height: 20px;
      letter-spacing: 0px;
    }
    .tag2 {
      height: 24px;
      background: #eceaea;
      border-radius: 4px;
      margin-left: 10px;
      text-align: center;
      font-size: 14px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      color: #545671;
      line-height: 20px;
      letter-spacing: 0px;
    }
    .chart-item {
      display: flex;
      align-items: center;
      padding-top: 20px;
      .c1 {
        width: 50px;
        font-size: 14px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: left;
        color: #545671;
        line-height: 20px;
        letter-spacing: 0px;
      }
      .c2 {
        flex: 1;
        color: #ffffff;
        font-size: 12px;
        background-color: #ebdfdf;
        border-radius: 5px;
        .num {
          border-radius: 5px;
          background-color: #fe6d67;
          text-align: right;
          padding-right: 10px;
        }
      }
      .c3 {
        width: 40px;
        text-align: center;
        .down {
          color: #fe6d67;
        }
        .up {
          color: #00d47a;
        }
      }
      .c4 {
        width: 30px;
        font-size: 14px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: right;
        color: #5b5d75;
        line-height: 20px;
        letter-spacing: 0px;
      }
    }
    .look-more {
      text-align: center;
      margin-top: 15px;
      font-size: 14px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: center;
      color: #545671;
      line-height: 20px;
      letter-spacing: 0px;
      i {
        color: #bebec6;
        font-size: 12px;
        padding-left: 5px;
      }
      .m2 {
        i {
          display: inline-block;
          transform: rotate(180deg);
        }
      }
    }
  }
}
</style>
